package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationCountDto;
import com.zzyl.dto.VisitDto;
import com.zzyl.entity.ReservationCount;
import com.zzyl.entity.Visit;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationCountVo;
import com.zzyl.vo.VisitVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @param
 * @return
 */
@Slf4j
@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {


    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    //查询取消预约数量
    @Override
    public Long selectCancelCount() {
        return customerReservationMapper.selectCancelCount(UserThreadLocal.getUserId());
    }

    //新增预约
    @Override
    public void insertReservation(VisitDto visitDto) {
        Visit visit = BeanUtil.toBean(visitDto, Visit.class);
        visit.setCreateBy(UserThreadLocal.getUserId());
        visit.setStatus(0);
        customerReservationMapper.insertReservation(visit);
    }

    //分页查询预约
    @Override
    public PageResponse pageReservation(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<VisitVo> visitVos = customerReservationMapper.pageReservation(status);
        PageResponse<VisitVo> of = PageResponse.of(visitVos, VisitVo.class);
        return of;
    }

    //取消预约
    @Override
    public void cancelReservation(Integer id) {
        customerReservationMapper.cancelReservation(id);

    }

    @Scheduled(cron = "0 1,31 * * * ?")
    public void executeTask(){
        log.info("定时任务开始执行：{}", LocalDateTime.now());
    }
    //查询每个时间段剩余预约次数
    @Override
    public List<ReservationCountVo> selectCountByTime(ReservationCountDto reservationCountDto) {

        ReservationCount reservationCount = BeanUtil.toBean(reservationCountDto, ReservationCount.class);
        List<ReservationCountVo> list = customerReservationMapper.selectCountByTime(reservationCount);
        return list;
    }
}
